package com.example.administrator.daydayappmarket.recommend.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.example.administrator.daydayappmarket.DownloadActivity;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.SearchActivity;
import com.example.administrator.daydayappmarket.recommend.fragment.RecommendTopFirstFragment;
import com.example.administrator.daydayappmarket.recommend.fragment.RecommendTopSecondFragment;
import com.example.administrator.daydayappmarket.software.fragment.adapter.ViewPagerFragmentAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class RecommendTopImageFragmentSecondActivity extends AppCompatActivity {

    @InjectView(R.id.activity_recommend_top_image_fragment_second_imageback)
    RelativeLayout imageback;
    @InjectView(R.id.activity_recommend_top_image_fragment_second_imagedownload)
    RelativeLayout imagedownload;
    @InjectView(R.id.activity_recommend_top_image_fragment_second_imagesearch)
    RelativeLayout imagesearch;
    @InjectView(R.id.activity_recommend_top_image_fragment_second_top_actionbar)
    RelativeLayout bar;
    @InjectView(R.id.activity_recommend_top_image_fragment_second_tablayout)
    TabLayout tablayout;
    @InjectView(R.id.activity_recommend_top_image_fragment_second)
    RelativeLayout fragmentSecond;
    @InjectView(R.id.activity_recommend_top_image_fragment_second_viewPager)
    ViewPager viewPager;
    @InjectView(R.id.activity_recommend_top_image_fragment_second_text_title)
    TextView textTitle;

    private List<Fragment> fragmentList;
    private FragmentManager fragmentManager;
    private RequestQueue requestQueue;
    private StringRequest stringRequest;
    private String url;
    private ArrayList<String> titles;
    private ArrayList<String> ids;
    private MyReceiver receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_recommend_top_image_fragment_second);
        ButterKnife.inject(this);
        titles = new ArrayList<>();
        ids = new ArrayList<>();
        fragmentList = new ArrayList<>();
        fragmentManager = getSupportFragmentManager();

        Intent intent = getIntent();
        url = intent.getStringExtra("url");
        requestQueue = Volley.newRequestQueue(this);
        stringrequest();

        fragmentList.add(new RecommendTopFirstFragment());
        fragmentList.add(new RecommendTopSecondFragment());

        IntentFilter filter = new IntentFilter("com.appMarket.recommend.top.second.action.title");
        receiver = new MyReceiver();
        registerReceiver(receiver,filter);

    }

    private void inittablayoutViewPager() {
        ViewPagerFragmentAdapter adapter = new ViewPagerFragmentAdapter(fragmentManager,fragmentList,titles);
        viewPager.setAdapter(adapter);
        tablayout.setupWithViewPager(viewPager);
        Intent intent1 = new Intent("com.appMarket.recommend.top.second.action.first");
        intent1.putStringArrayListExtra("ids",ids);
        sendBroadcast(intent1);
//        View inflate =  LayoutInflater.from(this).inflate(R.layout.layout_tablayout_item, null);
//        TextView textView = (TextView) inflate.findViewById(R.id.layout_tablayout_item_text);
//        for (int i = 0;i<titles.size();i++){
//            TabLayout.Tab tab  = tablayout.getTabAt(i);
//            textView.setText(titles.get(i));
//            tab.setCustomView(inflate);
//        }

    }

    private void stringrequest() {
        stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONObject data = jsonObject.getJSONObject("data");
                    String title = data.getString("title");
                    textTitle.setText(title);
                    JSONArray applists = data.getJSONArray("applists");
                    for (int i = 0; i < applists.length(); i++) {
                        JSONObject jsonObject1 = applists.getJSONObject(i);
                        String title1 = jsonObject1.getString("title");
                        String id = jsonObject1.getString("id");
                        ids.add(id);
                        titles.add(title1);
                    }

                    Intent intent = new Intent("com.appMarket.recommend.top.second.action.title");
                    //Intent intent1 = new Intent("com.appMarket.recommend.top.second.action.first");
                    intent.putStringArrayListExtra("titles",titles);

//                    sendOrderedBroadcast(intent,null);
                     sendBroadcast(intent);
//                    inittablayoutViewPager();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(RecommendTopImageFragmentSecondActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
            }
        });

        stringRequest.setTag("tabrequest");
        requestQueue.add(stringRequest);
        requestQueue.start();
    }

    @OnClick({R.id.activity_recommend_top_image_fragment_second_imageback, R.id.activity_recommend_top_image_fragment_second_imagedownload, R.id.activity_recommend_top_image_fragment_second_imagesearch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.activity_recommend_top_image_fragment_second_imageback:
                finish();
                break;
            case R.id.activity_recommend_top_image_fragment_second_imagedownload:
                Intent intent = new Intent(this, DownloadActivity.class);
                startActivity(intent);
                break;
            case R.id.activity_recommend_top_image_fragment_second_imagesearch:
                Intent intent1 = new Intent(this, SearchActivity.class);
                startActivity(intent1);
                break;
        }
    }


    class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            intent.getStringArrayListExtra("title");
            inittablayoutViewPager();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        requestQueue.cancelAll("tabrequest");
        unregisterReceiver(receiver);

    }


}
